package com.qb.service.impl;

import com.qb.mapper.BlogMapper;
import com.qb.mapper.BtypeMapper;
import com.qb.pojo.Blog;
import com.qb.pojo.BlogExample;
import com.qb.pojo.Btype;
import com.qb.pojo.BtypeExample;
import com.qb.service.BtypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class BtypeServiceImpl implements BtypeService {

    @Autowired
    private BtypeMapper btypeMapper;
    @Autowired
    private BlogMapper blogMapper;

    //全查
    @Override
    public List<Btype> findAll() {
        List<Btype> btypes = btypeMapper.selectByExample(null);
        return btypes;
    }
    //小类别

    @Override
    public List<Btype> findSubTypeByTid(Integer typeid) {
        BtypeExample btypeExample = new BtypeExample();
        btypeExample.createCriteria().andTypePidEqualTo(typeid);
        List<Btype> btypes = btypeMapper.selectByExample(btypeExample);
        return btypes;
    }

    //数据回显
    @Override
    public Btype findById(Integer typeid) {
        return btypeMapper.selectByPrimaryKey(typeid);
    }

    //修改回显数据
    @Override
    public boolean updateType(Btype btype) {
        int flag = btypeMapper.updateByPrimaryKeySelective(btype);
        return flag > 0 ? true : false;
    }
    //删除大类  小类

    @Override
    public boolean deleteSubTypeBtn(Integer typeid) throws Exception {
        // 1：判断大类别下是否有小类别
        BtypeExample btypeExample = new BtypeExample();
        btypeExample.createCriteria().andTypePidEqualTo(typeid);
        List<Btype> list = btypeMapper.selectByExample(btypeExample);
        if (list != null && list.size() > 0) {//类别被引用
            throw new Exception("大类别被小类别引用");
        }
        //2:  判断当前的大类别是否被blog引用：
        BlogExample bolgExample = new BlogExample();
        bolgExample.createCriteria().andTypeFkEqualTo(typeid);
        List<Blog> bList = blogMapper.selectByExample(bolgExample);
        if (bList != null && bList.size() > 0) {
            throw new Exception("大类别被博客引用");
        }
        int flag = btypeMapper.deleteByPrimaryKey(typeid);
        return flag > 0 ? true : false;
    }
    //添加大类

    @Override
    public void save(Btype btype) {
        btypeMapper.insert(btype);
    }
}
